package com.xiaowu.netmusic.service.impl;

import com.baomidou.mybatisplus.core.conditions.query.LambdaQueryWrapper;
import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
import com.xiaowu.netmusic.domain.dto.PlaylistCreateDTO;
import com.xiaowu.netmusic.domain.po.Playlist;
import com.xiaowu.netmusic.domain.po.PlaylistSong;
import com.xiaowu.netmusic.domain.po.Song;
import com.xiaowu.netmusic.domain.vo.PlaylistDetailVO;
import com.xiaowu.netmusic.domain.vo.PlaylistVO;
import com.xiaowu.netmusic.domain.vo.SongVO;
import com.xiaowu.netmusic.mapper.PlaylistMapper;
import com.xiaowu.netmusic.mapper.PlaylistSongMapper;
import com.xiaowu.netmusic.mapper.SongMapper;
import com.xiaowu.netmusic.service.PlaylistService;
import com.xiaowu.netmusic.service.PlaylistSongService;
import com.xiaowu.netmusic.service.SongService;
import lombok.RequiredArgsConstructor;
import org.springframework.beans.BeanUtils;
import org.springframework.stereotype.Service;

import java.util.List;
import java.util.Objects;
import java.util.stream.Collectors;

/**
 * @description:
 * @author: xiaowu
 * @time: 2025/4/29 21:27
 */
@Service
@RequiredArgsConstructor
public class PlaylistServiceImpl extends ServiceImpl<PlaylistMapper, Playlist> implements PlaylistService {
    private final PlaylistMapper playlistMapper;
    private final PlaylistSongService playlistSongService;
    private final SongService songService;

    // 创建歌单
    public void createPlaylist(PlaylistCreateDTO dto, Long userId) {
        Playlist playlist = new Playlist();
        playlist.setName(dto.getName());
        playlist.setDescription(dto.getDescription());
        playlist.setUserId(userId);
        playlistMapper.insert(playlist); // 使用 MyBatis 插入歌单
    }

    // 获取指定用户的所有歌单
    public List<PlaylistVO> getPlaylistsByUser(Long userId) {
        List<Playlist> playlists = playlistMapper.selectList(
                new LambdaQueryWrapper<Playlist>().eq(Playlist::getUserId, userId)
        );
        return playlists.stream()
                .map(playlist -> {
                    PlaylistVO vo = new PlaylistVO();
                    BeanUtils.copyProperties(playlist, vo);
                    return vo;
                })
                .collect(Collectors.toList());
    }

    // 查看歌单的详细信息（包括歌曲列表）
    public PlaylistDetailVO getPlaylistDetail(Long playlistId) {
        Playlist playlist = playlistMapper.selectById(playlistId);
        if (playlist == null) {
            throw new RuntimeException("歌单不存在");
        }

        List<PlaylistSong> playlistSongs = playlistSongService.getSongsInPlaylist(playlistId);
        List<SongVO> songVOList = playlistSongs.stream()
                .map(playlistSong -> songService.getById(playlistSong.getSongId()))
                .filter(Objects::nonNull)
                .map(this::toVO)  // 转换成 VO
                .collect(Collectors.toList());

        PlaylistDetailVO detailVO = new PlaylistDetailVO();
        BeanUtils.copyProperties(playlist, detailVO);
        detailVO.setSongs(songVOList);  // 设置的是 List<SongVO>

        return detailVO;
    }


    // 删除歌单
    public void deletePlaylist(Long playlistId) {
        int rows = playlistMapper.deleteById(playlistId);
        if (rows == 0) {
            throw new RuntimeException("删除失败，歌单不存在");
        }
    }

    private SongVO toVO(Song song) {
        SongVO vo = new SongVO();
        BeanUtils.copyProperties(song, vo);
        return vo;
    }
}
